package com.music.peacerhythms.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.music.peacerhythms.R;
import com.music.peacerhythms.Uakj.Constants;
import com.music.peacerhythms.Uakj.PlayEvent;
import com.music.peacerhythms.Uakj.SoundPoolManager;
import com.music.peacerhythms.active.ConfigActivity;
import com.music.peacerhythms.active.FullscreenActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class WidgetProvider extends AppWidgetProvider {


    private final String ACTION_UPDATE_ALL = "android.appwidget.action.APPWIDGET_UPDATE";

    private static Set idsSet = new HashSet();

    public static int mIndex;
    public static boolean isplay;


    @Override
    public void onReceive(final Context context, Intent intent) {
        super.onReceive(context, intent);
        final String action = intent.getAction();
        if (action != null)
            Log.e("aaaa", action);
        if (ACTION_UPDATE_ALL.equals(action)) {

            updateAllAppWidgets(context, AppWidgetManager.getInstance(context), idsSet);
        } else if (intent.hasCategory(Intent.CATEGORY_ALTERNATIVE)) {

            mIndex = 0;

            updateAllAppWidgets(context, AppWidgetManager.getInstance(context), idsSet);
        }
        try {

            if (action.equals(Constants.ACTION_STOP)) {
                SoundPoolManager.ispalying=false;
                Intent serviceIntent = new Intent(context, WidgetService.class);
                context.stopService(serviceIntent);
                updateAllAppWidgets(context, AppWidgetManager.getInstance(context), idsSet);
            }


            if (action.equals(Constants.ACTION_PLAY)) {
                SoundPoolManager.ispalying=true;
                Intent serviceIntent = new Intent(context, WidgetService.class);
                context.startService(serviceIntent);
                updateAllAppWidgets(context, AppWidgetManager.getInstance(context), idsSet);
            }
        } catch (Exception e) {

        }


    }






    private void updateAllAppWidgets(Context context, AppWidgetManager appWidgetManager, Set set) {

        int appID;

        Iterator it = set.iterator();

        mIndex++;


        while (it.hasNext()) {
            appID = ((Integer) it.next()).intValue();


            RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.app_widget);

            if (SoundPoolManager.ispalying)
            remoteView.setImageViewResource(R.id.play,R.mipmap.suspend);
            else
                remoteView.setImageViewResource(R.id.play,R.mipmap.play);

            if (SoundPoolManager.ispalying)
                remoteView.setOnClickPendingIntent(R.id.play, getStopPendingIntent(context));
                else
                remoteView.setOnClickPendingIntent(R.id.play, getPlayPendingIntent(context));

            appWidgetManager.updateAppWidget(appID, remoteView);
        }
    }


    private PendingIntent getResetPendingIntent(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, WidgetProvider.class);
        intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
        return pi;
    }

    private PendingIntent getPlayPendingIntent(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, WidgetProvider.class);
        intent.addCategory(Constants.ACTION_PLAY);
        intent.setAction(Constants.ACTION_PLAY);
     //   intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
        try {
            EventBus.getDefault().post(new PlayEvent(true));
        }catch (Exception e){

        }
        PendingIntent pi ;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
            pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        } else {
            pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        }
        return pi;
    }

    private PendingIntent getStopPendingIntent(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, WidgetProvider.class);
        intent.addCategory(Constants.ACTION_STOP);
        intent.setAction(Constants.ACTION_STOP);
    //    intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
        PendingIntent pi ;
        try {
            EventBus.getDefault().post(new PlayEvent(false));
        }catch (Exception e){

        }

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
             pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        } else {
             pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        }
        return pi;
    }


    private PendingIntent getOpenPendingIntent(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, FullscreenActivity.class);

        PendingIntent pi ;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
            pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        } else {
            pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        }
        return pi;
    }


    @Override
    public void onEnabled(Context context) {

        super.onEnabled(context);
    }

    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle
            newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
    }


    @Override
    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
    }


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
       // super.onUpdate(context, appWidgetManager, appWidgetIds);


        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);


        if (SoundPoolManager.getAppManager().ispalying){
            remoteViews.setOnClickPendingIntent(R.id.play, getStopPendingIntent(context));
            remoteViews.setImageViewResource(R.id.play,R.mipmap.suspend);
        }else {
            remoteViews.setOnClickPendingIntent(R.id.play, getPlayPendingIntent(context));
            remoteViews.setImageViewResource(R.id.play,R.mipmap.play);
        }


        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
        for (int appWidgetId : appWidgetIds) {
            idsSet.add(Integer.valueOf(appWidgetId));
        }

    }


    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {

        for (int appWidgetId : appWidgetIds) {
            idsSet.remove(Integer.valueOf(appWidgetId));
        }
        super.onDeleted(context, appWidgetIds);
    }


    @Override
    public void onDisabled(Context context) {
        Intent intent = new Intent(context, WidgetService.class);
        context.stopService(intent);
        super.onDisabled(context);
    }
}